<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/3/29
 * Time: 14:31
 */
namespace app\controller\api;
use app\model\Quotation AS model;
use app\model\Material AS material;
use think\facade\Db;

class Goods extends Base{
    public function list(){
        $post = input('post.');
        if(!empty($post['type']) && $post['type'] == 1) {
            $model = Db::name('model')->field('model')->select()->toArray();
            if (!empty($model))
                $model = array_column($model,'model');
            return $this->msg($model);
        }elseif(!empty($post['type']) && $post['type'] == 2){
            $model = Db::name('forms')->field('forms')->select()->toArray();
            if (!empty($model))
                $model = array_column($model,'forms');
            return $this->msg($model);
        }
        $where = [];
        !empty($post['name']) && $where[] = ['name','like','%'.$post['name'].'%'];
        (!empty($post['model']) && $post['model'] != '全部') && $map[] = ['model','like','%'.$post['model'].'%'];
        (!empty($post['material']) && $post['material'] != '全部') && $where[] = ['forms','like',$post['material']];
        !empty($post['supplier']) && $where[] = ['supplier','like',$post['supplier']];
        !empty($post['weight']) && $where[] = ['weight','>',$post['weight']];
        !empty($post['attr']) && $where[] = ['attr','like','%'.$post['attr'].'%'];
        !empty($post['is_tax']) && $where[] = ['is_tax','=',$post['is_tax'] == '含税' ? 1 : 2];
        if (!empty($map)){
            $model = Db::name('model')->where($map)->select()->toArray();
            $model = array_column($model,'model');
            $where[] = ['model','IN',$model];
        }
        $model = new model();
        $list = $model
            ->is_new(true)
            ->where($where)
            ->where('stock','<>',0)
            ->where('stock','<>','')
            ->where('stock','NOT LIKE','无')
            ->limit(($this->page - 1) * 40,40)
            ->order('price ASC')
            ->select();
        return $this->msg($list);
    }

    public function material(){
        $post = input('post.');
        $where = [];
        $group = '';
        $field = '*';
        !empty($post['name']) && $where[] = ['name','like','%'.$post['name'].'%'];
        !empty($post['quality']) && $where[] = ['quality','like','%'.$post['quality'].'%'];
        !empty($post['attr']) && $where[] = ['attr','like','%'.$post['attr'].'%'];
        !empty($post['supplier']) && $where[] = ['supplier','like','%'.$post['supplier'].'%'];
        if(!empty($post['group'])){
            $group = $post['group'];
            $field = $post['group'].',count(0) AS count';
        }
        $model = new material();
        $list = $model
            ->is_new(true)
            ->field($field)
            ->where($where)
            ->group($group)
            ->limit(20)
            ->select();
        return $this->msg($list);
    }
}